
package fr.mehfile.api.update.tasks;


import android.content.Context;
import fr.mehfile.api.parser.UpdateData;
import fr.mehfile.api.update.Tasks;
import fr.mehfile.api.update.TasksListener;
import fr.mehfile.api.update.settings.UpdateManagerSettings;
import fr.mehfile.view.utils.Utils;

public class SettingsUpdateTask extends Tasks {

    private static final String TAG = "SettingsUpdateTask";
    private UpdateData updateData;
    private Context context;
    private Exception error = null;
    private UpdateManagerSettings updateManagerSettings;

    public UpdateData getUpdateData() {
        return updateData;
    }

    public SettingsUpdateTask(Context context, TasksListener listener,
            UpdateManagerSettings updateManagerSettings, UpdateData updateData) {
        super(Tasks.SETTINGS_UPDATE_TASK_ID, listener);
        this.updateData = updateData;
        this.context = context;
        this.updateManagerSettings = updateManagerSettings;
    }

    @Override
    protected Void doInBackground(Object... params) {
        Utils.debugger.logDebug(TAG, "Updating settings..");

        // Xml version.
        int xmlVersion = updateData.getXmlversion();
        Utils.debugger.logDebug(TAG, "Set xml version to :" + xmlVersion);
        updateManagerSettings.setXmlVersion(xmlVersion);

        // Server URL.
        String serverUrl = updateData.getServerUrl().toString();
        Utils.debugger.logDebug(TAG, "Set Server URL to:" + serverUrl);
        updateManagerSettings.setUrlServer(serverUrl);

        return null;
    }

    @Override
    protected void onPostExecute(Object result) {
        if (getListener() != null) {
            if (error == null) {
                getListener().onComplete(this, null);
            } else {
                getListener().onError(this, error);
            }
        }
    }

}
